package org.lzfto.flink.demo.utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

/**
*通用的工具
*@author lzfto
*@创建时间 2025年6月20日 16:23:52
*/
public class Hand {

	/**
	 * map参数获取方法
	 * @param params 参数Map
	 * @param key 参数键
	 * @param clazz 目标类型 (String/Long/Integer/BigDecimal/BigInteger)
	 * @param defaultValue 默认值(可选)
	 * @return 转换后的参数值
	 * @throws IllegalArgumentException 如果参数不存在且无默认值，或转换失败
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getMapVal(Map<String, Object> params, String key, Class<T> clazz, T defaultValue) {
		Object value = params.get(key);
		if (value == null) {
			if (defaultValue != null) {
				return defaultValue;
			}
			throw new IllegalArgumentException("缺少必要键: " + key);
		}
		
		try {
			if (clazz == String.class) {
				return (T) value.toString();
			} else if (clazz == Long.class) {
				return (T) Long.valueOf(value.toString());
			} else if (clazz == Integer.class) {
				return (T) Integer.valueOf(value.toString());
			} else if (clazz == BigDecimal.class) {
				return (T) new BigDecimal(value.toString());
			} else if (clazz == BigInteger.class) {
				return (T) new BigInteger(value.toString());
			}
			throw new IllegalArgumentException("不支持的类型: " + clazz.getSimpleName());
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException(key + "必须是有效的" + clazz.getSimpleName());
		}
	}
}
